package com.app.tablet.data.provider;

import android.app.SearchManager;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.util.Log;

import com.app.tablet.data.db.TabletDB;

public class TabletProvider extends ContentProvider {

	private static final String TagProject = "Tablet-Project";
	private static final String TagPage = "TabletProvider-Page ::";

	private static final String AUTHORITY = "com.app.tablet.data.provider.tabletprovider";

	private static final String TABLETREMINDER_BASEPATH = "tabletreminder";
	public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
			+ "/" + TABLETREMINDER_BASEPATH);

	public static String TABLET_TABLE_OUERY = "select * from reminder";
	public static String TABLET_NAME_OUERY = "select med_name from reminder";
	public static String TABLET_NAME_SEARCH_OUERY = "select * from reminder where med_name LIKE ' %s '";

	public static String TABLE_NAME = "reminder";

	private static final int TABLET_NAME = 1;
	private static final int DOSAGE = 2;
	private static final int SCHEDULE = 3;
	private static final int TABLE_ALL = 4;

	private static final int SEARCH_WORDS = 5;
	private static final int GET_WORD = 6;
	private static final int SEARCH_SUGGEST = 7;

	private static final UriMatcher s_urlMatcher = new UriMatcher(
			UriMatcher.NO_MATCH);

	static {

		s_urlMatcher.addURI("com.app.tablet.data.provider.tabletprovider","med_name", TABLET_NAME);
		s_urlMatcher.addURI("com.app.tablet.data.provider.tabletprovider","dosage", DOSAGE);
		s_urlMatcher.addURI("com.app.tablet.data.provider.tabletprovider","all", TABLE_ALL);
		s_urlMatcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY,SEARCH_SUGGEST);
		s_urlMatcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY+ "/*", SEARCH_SUGGEST);

	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public String getType(Uri uri) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		Log.i(TagProject, TagPage + "Insert code ...." + uri);
		Uri result = null;
		SQLiteDatabase dbObj = mytabletdb
				.getConnection(SQLiteDatabase.OPEN_READWRITE);
		dbObj.insert("reminder", null, values);
		return result;
	}

	private TabletDB mytabletdb;

	@Override
	public boolean onCreate() {
		mytabletdb = new TabletDB(this.getContext());
		return true;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		Log.i(TagProject, TagPage + "The URI IS " + uri);
		int match = s_urlMatcher.match(uri);
		
		
		Cursor result = null;
		switch (match) {
		case TABLET_NAME:
			SQLiteDatabase dbObj = mytabletdb
			.getConnection(SQLiteDatabase.OPEN_READONLY);
			result = dbObj.rawQuery(TABLET_NAME_OUERY, null);
			result.setNotificationUri(getContext().getContentResolver(), uri);
			dbObj.close();
			break;
		case TABLE_ALL:
			SQLiteDatabase dbsObj = mytabletdb
			.getConnection(SQLiteDatabase.OPEN_READONLY);
			result = dbsObj.rawQuery(TABLET_TABLE_OUERY, null);
			result.setNotificationUri(getContext().getContentResolver(), uri);
			dbsObj.close();
			break;
		case SEARCH_SUGGEST:
			String searchdata=selectionArgs[0];
			String seearchQuery =String.format(TABLET_NAME_SEARCH_OUERY, searchdata);
			Log.i(TagProject,TagPage+"The Search SQL STATEMENT is :"+seearchQuery);
			SQLiteDatabase dbjObj = mytabletdb
					.getConnection(SQLiteDatabase.OPEN_READONLY);
			result = dbjObj.rawQuery(seearchQuery, null);
			dbjObj.close();
			break;

		}

		return result;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		// TODO Auto-generated method stub
		return 0;
	}

}
